#include "io430.h"

void delay(unsigned int n) {
  while (n > 0) n--;
}

// RC capacitive sensor on P2.6 discharging into P2.7
int touch1() {
  unsigned int t1 = 0;        // start of discharge interval
  unsigned int t2 = 0;        // end of discharge interval  

  // Set P2.7 low
  ... 
  // Set P2.6 high to charge Ctouch1 to VDD
  ...
  // Wait a brief period of time to ensure Ctouch1 is fully charged
  delay(10);
  // Charge the direction of P2.6 to input
  ...
  // Store the current time in variable t1
  ...
  // Wait for P2.6 to go low
  ...
  // Store the current time in variable t2
  ...
  // The discharge time equals t2-t1
  return t2-t1;
}

int main(void){
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  // maximum clock speed
  BCSCTL3 = LFXT1S_2;       // frees up pins
  BCSCTL1 = CALBC1_16MHZ;
  DCOCTL = CALDCO_16MHZ;
  
  // turn on timer A free running (or whatever this does)
  TACTL = TASSEL_2 + MC_2;

  // P1 is feedback
  P1DIR = 0xff;             // P1 output
  P1SEL = 0;                // digital out

  // configure P2 for capacitive sense
  P2OUT = 0;                // P2 output low
  P2DIR = BIT6 + BIT7;      // P2 is output
  P2SEL = 0;                // P2 is digital i/0
  P2REN = 0;                // P2 disable pullup resistors

  for (;;) {
    unsigned int T = touch1();
    // display T on LED bar
    ...
    // wait a little
    delay(100);
  }
}